<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="景区名称" prop="landscapesName">
        <el-input v-model="queryParams.landscapesName" placeholder="请输入山水名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['view:landscapes_introduction:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['view:landscapes_introduction:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['view:landscapes_introduction:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['view:landscapes_introduction:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表单 -->
    <el-table v-loading="loading" :data="landscapes_introductionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="景区标题" align="center" prop="title" />
      <el-table-column label="景区名称" align="center" prop="landscapesName" />
      <el-table-column label="景区图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-image v-for="(item, index) in scope.row.privImg" :key="index" :src="item"
            :preview-src-list="scope.row.privImg" :lazy="true" alt style="width:50px;height:50px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="景区规模" align="center" prop="landscapeScaleName"/>
      <el-table-column label="景区介绍" align="center" prop="landscapesIntroduction">
        <template slot-scope="scope">
          <div :title="scope.row.landscapesIntroduction" v-html="scope.row.landscapesIntroduction"
            style="height:100px;overflow:hidden"></div>
        </template>
      </el-table-column>
      <el-table-column label="景区地址" align="center" prop="address" />
      <el-table-column label="所属乡镇" align="center" prop="townShipName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['view:landscapes_introduction:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['view:landscapes_introduction:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改景区信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1200px" append-to-body
      @close="dioClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="景区标题" prop="title">
          <el-input type="text" v-model="form.title" placeholder="请输入景区标题"></el-input>
        </el-form-item>
        <el-form-item label="景区名称" prop="landscapesName">
          <el-input type="text" v-model="form.landscapesName" placeholder="请输入景区名称"></el-input>
        </el-form-item>
        <el-form-item label="景区分类" prop="landscapeClassify">
          <el-select filterable v-model="form.landscapeClassify" placeholder="请选择景区分类" multiple clearable size="small">
            <el-option v-for="dict in scenicTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="景区规模" prop="landscapeScale">
          <el-select filterable v-model="form.landscapeScale" placeholder="请选择景区规模" clearable size="small">
            <el-option v-for="dict in landscapeScaleOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属乡镇" prop="townShipId">
          <el-select filterable v-model="form.townShipId" placeholder="请选择所属乡镇" clearable size="small"
            @change="townShipIdChange">
            <el-option v-for="dict in comprehensive_displayList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属乡村" prop="villageId">
          <el-select filterable v-model="form.villageId" placeholder="请选择所属乡村" clearable size="small">
            <el-option v-for="item in villageList" :key="item.villageId" :label="item.villageName"
              :value="item.villageId" />
          </el-select>
        </el-form-item>
        <el-form-item label="景区图片" prop="image">
          <imageUpload v-model="form.image" @uploadImage="uploadimg" @remove="removeImg" :limit="6" />
        </el-form-item>
        <el-form-item label="景区介绍" prop="landscapesIntroduction">
          <editor v-if="open" v-model="form.landscapesIntroduction" :min-height="192" />
        </el-form-item>
        <el-form-item label="景区地址" prop="address">
          <zxMap :value="latLng" @input="mapClick" :address="form.address" v-if="open"></zxMap>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" disabled />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLandscapes_introduction,
  getLandscapes_introduction,
  delLandscapes_introduction,
  addLandscapes_introduction,
  updateLandscapes_introduction,
  exportLandscapes_introduction,
  listVillage,
} from "@/api/view/landscapes_introduction";
import { town_list } from "@/api/view/activity_information";
import ImageUpload from "@/components/UploadImage";
import zxMap from "@/components/map/index";
import Editor from "@/components/Editor";
export default {
  name: "Landscapes_introduction",
  components: {
    ImageUpload,
    Editor,
    zxMap,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 山水介绍表格数据
      landscapes_introductionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      comprehensive_displayList: [],
      // 标签字典
      labelOptions: [],
      // 景区规模字典
      landscapeScaleOptions: [],
      // 景区分类字典
      scenicTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        landscapesName: null,
        landscapesIntroduction: null,
        label: null,
        comprehensiveDisplayId: null,
      },
      queryComprehensive: {
        pageNum: 1,
        pageSize: 99999,
        category: 2,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入景区标题", trigger: "blur" }],
        landscapesName: [{ required: true, message: "请输入景区名称", trigger: "blur" }],
        landscapeScale: [{ required: true, message: "请选择景区规模", trigger: "change" }],
        townShipId: [{ required: true, message: "请选择", trigger: "blur" }],
        image: [{ required: true, message: "请选择", trigger: "blur" }],
        address: [{ required: true, message: "请选择", trigger: "blur" }],
        landscapesIntroduction: [{ required: true, message: "请输入", trigger: "blur" }],
        latitude: [{ required: true, message: "请在上方地图中选择", trigger: "blur" }],
        longitude: [{ required: true, message: "请在上方地图中选择", trigger: "blur" }],
        landscapeClassify: [{ required: true, message: "请选择景区分类", trigger: "blur" }],
        villageId: [{ required: true, message: "请选择所属乡村", trigger: "blur" }],
      },
      latLng: "",
      //乡村列表
      villageList: [],
    };
  },
  created() {
    if (this.$route.query.searchName) {
      this.queryParams.landscapesName = this.$route.query.searchName;
      this.getList();
    };
    this.getList();
    this.getComList();
    this.getDicts("scenery_tag").then((response) => {
      this.labelOptions = response.data;
    });
    this.getDicts("landscape_classify").then((response) => {
      this.scenicTypeOptions = response.data;
    });
    this.getDicts("landscape_scale").then((response) => {
      this.landscapeScaleOptions = response.data;
    });
  },
  methods: {
    /** 查询乡镇列表 */
    getComList() {
      town_list().then((res) => {
        this.comprehensive_displayList = res.data;
      });
    },
    /** 查询景区列表 */
    getList() {
      this.loading = true;
      listLandscapes_introduction(this.queryParams).then((response) => {
        response.rows.forEach(item => {
        if(item.imageScale){
          let imgArr = [];
          item.imagesUrl = item.imageScale.split(",");
          item.imagesUrl.forEach(val => {
            imgArr.push(this.baseurl + val);
            item.privImg = imgArr;
          });
          let res= this.landscapeScaleOptions.find(val => val.dictValue == item.landscapeScale);
          if(res){
            item.landscapeScaleName = res.dictLabel;
          }
        } else{
          return
        }

        });
        this.landscapes_introductionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        landscapesName: null,
        landscapesIntroduction: null,
        image: null,
        label: null,
        comprehensiveDisplayId: null,
        longitude: null,
        latitude: null,
        address: null,
        landscapeClassify: null,
        villageId: null,
        landscapeScale: null,
        title: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加景区";
    },

    //所属乡镇el-select change事件
    async townShipIdChange(id) {
      if (id) {
        const res = await listVillage({ id: id });
        this.villageList = res.data;
      };

    },

    /** 查询乡村列表 */

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      this.reset();
      this.title = "修改景区";
      getLandscapes_introduction(id).then(async res => {
        console.log(res, 'upload');
        let response = await listVillage({ id: res.data.townShipId });
        this.latLng = res.data.longitude + "," + res.data.latitude;
        res.data.landscapeClassify = res.data.landscapeClassify ? res.data.landscapeClassify.split(",") : [];
        res.data.villageId = Number(res.data.villageId);
        this.villageList = response.data;
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.landscapeClassify = data.landscapeClassify ? data.landscapeClassify.toString() : "";
          if (this.form.id != null) {
            updateLandscapes_introduction(data).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLandscapes_introduction(data).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除景区名称为"' + row.landscapesName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delLandscapes_introduction(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有山水介绍数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportLandscapes_introduction(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    //图片上传事件
    uploadimg(e) {
      this.form.image = e;
    },
    //删除图片事件
    removeImg(e) {
      this.form.image = e;
    },
    /* 地图事件 */
    mapClick(e) {
      let latlng = e.lnglat.split(",");
      this.form.longitude = latlng[0];
      this.form.latitude = latlng[1];
      this.form.address = e.address;
      console.log(this.form, "emap");
    },
    /* dio关闭时触发 */
    dioClose() {
      this.latLng = "";
    },
  },
};
</script>
<style lang="scss">
.el-select {
  width: 100%;
}
</style>
